package counter;

import main.*;
import barrier.*;

public class CounterWithBarrier extends Counter {
	private Barrier _barrier; // all barriers answer interface Barrier
//	private ThreadLocal<Integer> _threadId;
	private StringBuffer _enterLeaveTimes;  
	
	/**
	 * @param barrierType which barrier to use
	 */
	public CounterWithBarrier(BarrierType barrierType, final int numOfThreads) {
		_barrier = GeneralUtils.getBarrier(barrierType, numOfThreads); 
//	  _threadId = new ThreadLocal<Integer>() {
//	  @Override
//	  protected Integer initialValue() { 
//	    return ThreadID.get() % Main.NUM_OF_THREADS; };
//	  };
	  
	  _enterLeaveTimes = new StringBuffer();
//	  _enterLeaveTimes = new StringBuffer(barrierType.toString()).append(":\n");
	}
	
	/**
	 * Awaits and report how long did thread waited.
	 */
	public void await() {	
//	  long enterTime = System.nanoTime();
//	  int me = _threadId.get();
//	  String msg = "Entering: Thread ," + me + " ,at, " + enterTime/1000000
//	    + " ,millies\n";
//	  System.out.print(msg);
//	  _enterLeaveTimes.append(msg);
	  
	  _barrier.await();
	  
//	  long leftTime = (new Long(System.nanoTime() - enterTime));
//	  msg = "Leaving: Thread," + _threadId.get() +	", ,waited: ," 
//	    + leftTime/1000000 + ", millies\n";
//	  System.out.print(msg);
//	  _enterLeaveTimes.append(msg);
	}

	public StringBuffer getBuf() {
	  return _enterLeaveTimes;
	}
}
